/*
 * @Author: Kiro AI Assistant
 * @Date: 2025-01-10
 * @Description: 音乐搜索页面测试
 */

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:provider/provider.dart';

import '../../lib/pages/music_search_page.dart';
import '../../lib/viewmodels/music_search_view_model.dart';
import '../../lib/viewmodels/music_player_view_model.dart';

void main() {
  group('MusicSearchPage Tests', () {
    late MusicSearchViewModel searchViewModel;
    late MusicPlayerViewModel playerViewModel;

    setUp(() {
      searchViewModel = MusicSearchViewModel();
      playerViewModel = MusicPlayerViewModel();
    });

    tearDown(() {
      searchViewModel.dispose();
      playerViewModel.dispose();
    });

    Widget createTestWidget() {
      return MultiProvider(
        providers: [
          ChangeNotifierProvider.value(value: searchViewModel),
          ChangeNotifierProvider.value(value: playerViewModel),
        ],
        child: const MaterialApp(
          home: MusicSearchPage(),
        ),
      );
    }

    testWidgets('应该显示搜索页面的基本UI元素', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 验证AppBar存在
      expect(find.byType(AppBar), findsOneWidget);

      // 验证返回按钮存在
      expect(find.byIcon(Icons.arrow_back), findsOneWidget);

      // 验证搜索框存在
      expect(find.byType(TextField), findsOneWidget);

      // 验证搜索图标存在
      expect(find.byIcon(Icons.search), findsOneWidget);
    });

    testWidgets('应该显示搜索提示文本', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 验证搜索提示文本
      expect(find.text('搜索音乐、歌手、专辑'), findsOneWidget);
    });

    testWidgets('初始状态应该显示搜索历史', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());
      await tester.pumpAndSettle();

      // 由于初始状态没有搜索历史，应该显示热门搜索
      // 这里可以根据实际的热门搜索数据进行验证
    });

    testWidgets('点击返回按钮应该关闭页面', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 点击返回按钮
      await tester.tap(find.byIcon(Icons.arrow_back));
      await tester.pumpAndSettle();

      // 验证页面已关闭（在实际应用中，这会导航回上一页）
    });

    testWidgets('输入搜索内容后应该显示清除按钮', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 在搜索框中输入文本
      await tester.enterText(find.byType(TextField), '测试搜索');
      await tester.pump();

      // 验证清除按钮出现
      expect(find.byIcon(Icons.clear), findsOneWidget);
    });

    testWidgets('点击清除按钮应该清空搜索框', (WidgetTester tester) async {
      await tester.pumpWidget(createTestWidget());

      // 在搜索框中输入文本
      await tester.enterText(find.byType(TextField), '测试搜索');
      await tester.pump();

      // 点击清除按钮
      await tester.tap(find.byIcon(Icons.clear));
      await tester.pump();

      // 验证搜索框已清空
      expect(find.text('测试搜索'), findsNothing);
    });
  });

  group('MusicSearchPage Integration Tests', () {
    testWidgets('搜索流程集成测试', (WidgetTester tester) async {
      final searchViewModel = MusicSearchViewModel();
      final playerViewModel = MusicPlayerViewModel();

      await tester.pumpWidget(
        MultiProvider(
          providers: [
            ChangeNotifierProvider.value(value: searchViewModel),
            ChangeNotifierProvider.value(value: playerViewModel),
          ],
          child: const MaterialApp(
            home: MusicSearchPage(),
          ),
        ),
      );

      // 1. 验证初始状态
      expect(find.byType(TextField), findsOneWidget);

      // 2. 输入搜索关键词
      await tester.enterText(find.byType(TextField), '周杰伦');
      await tester.pump();

      // 3. 提交搜索
      await tester.testTextInput.receiveAction(TextInputAction.search);
      await tester.pump();

      // 4. 验证搜索状态（这里应该显示加载状态）
      // 由于没有真实的网络请求，这里主要验证UI状态变化

      // 清理资源
      searchViewModel.dispose();
      playerViewModel.dispose();
    });
  });
}
